<?php
defined('IN_RONCRM') or exit('No permission resources.');
class IndexController extends CAdmin{
	
	function __construct(){
		parent::__construct();
	}

	public function init(){
		$userid = $_SESSION['userid'];
		$admin_menu = CAdmin::admin_menu('0');
		$realname = CParam::get_cookie('realname');
		include self::admin_tpl('index');
	}
	
	public function login(){
		if(isset($_POST['dosubmit'])){
			if(!isset($_POST['username']) || empty($_POST['username'])) self::showmessage('用户名不能为空','?m=admin&c=index&a=login');
			$username = trim($_POST['username']);
			$admin_db = new Admin_Model();
			$admin = $admin_db->get_one(array('username'=>$username));
			if(!$admin) self::showmessage('用户名或密码不正确','?m=admin&c=index&a=login');
			$password = md5(md5(trim($_POST['password'])).$admin['encrypt']);
			if($password != $admin['password']){
				self::showmessage('用户名或密码不正确','?m=admin&c=index&a=login');
			}
			$sysfuntion = new CSysfunction();
			$process = new CProcess();
			$admin_db->update(array('lastloginip'=>$sysfuntion->get_ip(),'lastlogintime'=>SYS_TIME),array('userid'=>$admin['userid']));
			$_SESSION['userid'] = $admin['userid'];
			$_SESSION['roleid'] = $admin['roleid'];			
			$_SESSION['ron_hash'] = $process->random(6,'abcdefghigklmnopqrstuvwxwyABCDEFGHIGKLMNOPQRSTUVWXWY0123456789');
			$cookie_time = SYS_TIME+86400*30;
			CParam::set_cookie('admin_username',$username,$cookie_time);
			CParam::set_cookie('userid', $admin['userid'],$cookie_time);
			CParam::set_cookie('realname', $admin['realname'],$cookie_time);
			self::showmessage('登陆成功','?m=admin&c=index');
		}else{
			include self::admin_tpl('login');
		}
	}

	public function login_out(){
		$_SESSION['userid'] = 0;
		$_SESSION['roleid'] = 0;
		CParam::set_cookie('admin_username', '');
		CParam::set_cookie('userid', 0);
		self::showmessage('您已成功退出!','?m=admin&c=index&a=login');
	}

	
	/**
	 * 左侧菜单
	 */
	public function public_menu_left(){
		$menuid = intval($_GET['menuid']);
		$menu_left = CAdmin::admin_menu($menuid);
		include self::admin_tpl('left');
	}
}